package view;

import bean.Member;

import dao.MemberDao;
import impl.MemberDaoImpl;
import service.MemberService;
import service.impl.MemberServiceImpl;
import util.InputUtil;
import util.ServerResponseResult;

/**
 * @classname:MemberView
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/31 21:35
 * @version: 0.1
 * @since:1.8
 */
public class MemberView {
    private static int result;
    private static final MemberDao memberDao;

    static {
        memberDao = new MemberDaoImpl();
    }

    private MemberView() {
    }

    public static ServerResponseResult addMember() {
        Member member = new Member();
        System.out.println("请输入要添加的会员信息");
        System.out.println("请输入名字");
        member.setMname(InputUtil.inputNextStr());
        System.out.println("请输入密码");
        String pass = InputUtil.inputNextStr();
        member.setPass(pass);
        System.out.println("请输入上传照片路径");
        member.setImage(InputUtil.inputNextStr());
        System.out.println("请输入会员联系电话");
        member.setContact(InputUtil.inputNextStr());
        MemberService memberService = new MemberServiceImpl();
        ServerResponseResult serverResponseResult = memberService.addMember(member);
        return serverResponseResult;
    }

    public static void selectAll() {
        System.out.println("全部会员信息");
        showAll();
    }

    public static int deleteMemberById() {
        showAll();
        System.out.println("请输入要删除会员的id");
        int id = InputUtil.inputInt();
        result = memberDao.deleteMemberById(id);
        return result;
    }

    public static int updateMemberBalance() {
        showAll();
        System.out.println("请输入要充值的会员账号id");
        int id = InputUtil.inputInt();
        System.out.println("请输入要充值的金额");
        int money = InputUtil.inputInt();
        result = memberDao.updateMemberBalance(id, money);
        return result;
    }


    public static ServerResponseResult updateMemberInfo() {
        showAll();
        System.out.println("请选择要更改的用户Id");
        int id = InputUtil.inputInt();
        Member member = memberDao.selectMemberById(id);
        System.out.println("该用户信息如下");
        System.out.println(member);
        System.out.println("请选择要修改的内容 \t" +
                "1.名字 2.密码 3.头像 4.联系方式 5.积分 6.余额");
        String choice = InputUtil.inputNextStr();
        String[] splitsTr = choice.split(",");
        for (String s : splitsTr) {
            int num = Integer.parseInt(s);
            switch (num) {
                case 1:
                    System.out.println("请输入更改后名字");
                    member.setMname(InputUtil.inputNextStr());
                    break;
                case 2:
                    System.out.println("请输入更改后的密码");
                    member.setPass(InputUtil.inputNextStr());
                    break;
                case 3:
                    System.out.println("请输入更改后图片路径");
                    member.setImage(InputUtil.inputNextStr());
                    break;
                case 4:
                    System.out.println("请输入更改后联系电话");
                    member.setPass(InputUtil.inputNextStr());
                    break;
                case 5:
                    System.out.println("请输入更改后的积分");
                    member.setPoints(InputUtil.inputFloat());
                    break;
                case 6:
                    System.out.println("请输入更改后的余额");
                    member.setBalance(InputUtil.inputBigDecimal());
                    break;
            }
        }
        int i = memberDao.updateMemberById(member, id);
        if (i == 1) {
            return ServerResponseResult.success();
        }
        return ServerResponseResult.error();
    }

    public static void showAll() {
        memberDao.selectAll().forEach(System.out::println);
    }
}
